package cn.tj.wenda.cloud.other.rest;

import cn.tj.wenda.cloud.client.FallbackProxy;
import cn.tj.wenda.cloud.entity.ResponseBean;
import cn.tj.wenda.cloud.other.entity.Books;
import feign.hystrix.FallbackFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

@FeignClient(name="other-core",path = "/api",fallbackFactory = BookRestFuse.class)
@RequestMapping(value = "/other/book")
@Api(tags="主要用来管理图书信息")
public interface BookRest {

	@ApiOperation(value="取得所有图书一览",notes="")
    @ApiImplicitParams({            
    	@ApiImplicitParam(name="page",value="分页索引",required=false,dataType="Integer"),
    	@ApiImplicitParam(name="limit",value="返回记录数，为0时返回所有记录",required=false,dataType="Integer"),
    	@ApiImplicitParam(name="params",value="检索条件（JSON格式）",required=false,dataType="String")
    })
    @GetMapping("")
    public ResponseBean getBookList(@RequestParam("page") int page
            ,@RequestParam("limit") int limit
            , @RequestParam("params") String params);

	@ApiOperation(value="更新一条图书数据",notes="根据url的id来指定更新对象，并根据传过来的book信息来更新图书详细信息")
    @ApiImplicitParams({            
    	@ApiImplicitParam(name="bookid",value="图书ID",required=true,dataType="String"),
    	@ApiImplicitParam(name="book",value="图书实体",required=true,dataType="Books")
    })
    @PutMapping("/{bookid}")
    public ResponseBean updateBook(@PathVariable(name="bookid",required = true) String bookId,@RequestBody Books book);

	@ApiOperation(value="添加一条图书数据",notes="根据Books对象创建租户")
    @ApiImplicitParam(name="book",value="图书详细实体book",required=true,dataType="Books")
    @PostMapping("")
    public ResponseBean insertBook(@RequestBody Books book);

	@ApiOperation(value="删除一条图书数据",notes="根据url的bookid来指定删除对象")
    @ApiImplicitParam(name="bookid",value="图书ID",required=true,dataType="String")
    @DeleteMapping("/{bookid}")
    public ResponseBean deleteBook(@PathVariable("bookid") String bookId);
}

/**
 * 中断工厂类
 */
@Component
class BookRestFuse implements FallbackFactory<BookRest>{

    private String microName = "other-core";

    @Override
    public BookRest create(Throwable cause) {
        BookRest client = (BookRest) FallbackProxy.newInstance(microName,BookRest.class,cause);
        return client;
    }
}
